package com.lujianfei.sugarorm;

import android.content.Context;
import android.util.Log;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import static org.junit.Assert.assertEquals;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    private static final String TAG = "ExampleInstrumentedTest";

    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.lujianfei.sugarorm", appContext.getPackageName());
    }

    @Test
    public void testQueryAll() {
        List<Entity> entities = Entity.listAll(Entity.class);
        System.out.println("================= testQueryAll =================");
        System.out.println("" + entities);
    }

    @Test
    public void testQueryAllByName() {
        List<Entity> entities = Entity.find(Entity.class,"name like ?","John");
        System.out.println("================= testQueryAllByName =================");
        System.out.println("" + entities);
    }

    @Test
    public void testDeleteAllByName() {
        int affectedRows = Entity.deleteAll(Entity.class,"name like ?","John");
        System.out.println("================= testDeleteAllByName =================");
        System.out.println("affected " + affectedRows+ " rows");
    }

    @Test
    public void testModifyName() {
        List<Entity> entities = Entity.listAll(Entity.class);
        for (Entity entity:
        entities) {
            entity.setName("John");
            entity.setUpdateTime(System.currentTimeMillis());
            entity.save();
        }
        System.out.println("================= testModifyName =================");
    }

    @Test
    public void testAdd(){
        Entity saveEntity = new Entity();
        saveEntity.setName("Peter");
        saveEntity.setUpdateTime(System.currentTimeMillis());
        long id = saveEntity.save();
        System.out.println("================= testAdd =====================");
        System.out.println(saveEntity.toString()+" id="+id);
    }


}
